﻿/*****************************************************************************
 * Hyldahl.Services.TaskProcessing.Test            Created by: MSH 2008.07.22
 * --------------------------------------------------------------------------
 * FileName: Dummy\DummyWorker.cs
 * --------------------------------------------------------------------------
 * Copyright 2009 Martin Sixhøj Hyldahl
 *****************************************************************************/

using System;
using System.Threading;
using System.Diagnostics;
using Hyldahl.Services.TaskProcessing.Workers;

namespace Hyldahl.Services.TaskProcessing.Test.Dummy
{
    public class DummyWorker : Worker
    {
        /*****************************************************
         * CONSTRUCTOR
         *****************************************************/

        /// <summary>
        /// Initializes a new instance of the <see cref="DummyWorker"/> class.
        /// </summary>
        /// <param name="controller">The controller.</param>
        /// <param name="name">The name.</param>
        public DummyWorker(TaskProcessingController controller, string name)
            : base(controller, name)
        {
        }

        /*****************************************************
         * PRIVATE METHODS
         *****************************************************/

        /// <summary>
        /// Worker thread.
        /// </summary>
        protected override void WorkerThreadStart()
        {
            try
            {
                this.TraceSource.TraceInformation("Starting thread");

                while (true)
                {
                    DateTime timeout = DateTime.Now.AddMilliseconds(50);

                    while (DateTime.Now < timeout && !this.StopFlag)
                    {
                        Thread.Sleep(200);
                    }

                    if (this.StopFlag)
                        break;
                }
            }
            catch (Exception ex)
            {
                this.TraceSource.TraceEvent(TraceEventType.Error, 2, "Failure - {0}", ex.Message);
            }

            this.State = WorkerState.Stopped;

            this.TraceSource.TraceInformation("Stopped");
        }

        /// <summary>
        /// Gets or sets a value indicating whether [stop flag].
        /// </summary>
        /// <value><c>true</c> if [stop flag]; otherwise, <c>false</c>.</value>
        new public bool StopFlag
        {
            get
            {
                return base.StopFlag;
            }
        }
    }
}
